package com.collect.cameroon.zhiScene.zhiRemind.zhiRecall;

import android.annotation.SuppressLint;
import android.app.Activity;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiAdater.zhiRecall.ZHIRecallAdjuster;
import com.collect.cameroon.zhiBase.BaseFragment;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRMarkedCustomerFollowUserListEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRWaitCustomerFollowUserListEntity;
import com.collect.cameroon.MyApplication;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectConfigs;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.cameroon.zhiHelper.ZHISysShareHelper;
import com.collect.cameroon.zhiScene.zhiWidget.zhiSubmitRecTagView.ZHISubmitRecRecallView;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIRecallFragment extends BaseFragment<ZHIRecallPresenter> implements ZHIRecallContract.View {

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_query_count)
    TextView tv_query_count;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;
    @BindView(R.id.iv_products)
    ImageView iv_products;

    private ZHISubmitRecRecallView mZHISubmitRecRecallView;

    private ZHIRecallAdjuster mZHIRecallAdjuster = new ZHIRecallAdjuster();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recall;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            iv_products.setOnClickListener(v -> ZHIProjectRouter.toWordsSkills());

            mZHISubmitRecRecallView = new ZHISubmitRecRecallView(getActivity());
            mZHISubmitRecRecallView.setOnSubmitRecRecallItemClickListener(zhisMarkCustomerFollowUser -> {
                if (zhisMarkCustomerFollowUser != null && mPresenter != null) {
                    mPresenter.markCustomerFollowUser(getActivity(), zhisMarkCustomerFollowUser, true);
                }
            });

            initEvent();
            initData();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetLatestWaitCustomerFollowUserListV2(ArrayList<ZHIRWaitCustomerFollowUserListEntity> zhirWaitCustomerFollowUserListEntityArrayList) {
        try {
            srl_swipe_layout.setRefreshing(false);
            mZHIRecallAdjuster.setNewData(zhirWaitCustomerFollowUserListEntityArrayList);
//            tv_query_count.setText("Total:" + zhirWaitCustomerFollowUserListEntityArrayList.size());
            tv_page_title.setText(getString(R.string.tab_follow) + "(" + zhirWaitCustomerFollowUserListEntityArrayList.size() + ")");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetLatestMarkedCustomerFollowUserList(ArrayList<ZHIRMarkedCustomerFollowUserListEntity> zhirMarkedCustomerFollowUserListEntities) {

    }

    @Override
    public void onMarkCustomerFollowUser() {
        mZHISubmitRecRecallView.setRemarks("");
        mZHISubmitRecRecallView.dismiss();
        initData();
    }

    @SuppressLint("InflateParams")
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(this::initData);
        mZHIRecallAdjuster.setEmptyView(getRefreshEmptyView());
        rc_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_list.setAdapter(mZHIRecallAdjuster);

        mZHIRecallAdjuster.setOnZHIRecallAdjusterItemClickListener(new ZHIRecallAdjuster.OnZHIRecallAdjusterItemClickListener() {
            @Override
            public void OnItemClick(ZHIRWaitCustomerFollowUserListEntity zhirWaitCustomerFollowUserListEntity) {
                showSendSmsView(zhirWaitCustomerFollowUserListEntity);
            }

            @Override
            public void OnItemMobileClick(ZHIRWaitCustomerFollowUserListEntity zhirWaitCustomerFollowUserListEntity) {
                ZHISysShareHelper.callPhone(MyApplication.getAppContext(), zhirWaitCustomerFollowUserListEntity.getMobile());
            }

            @Override
            public void OnItemWhatsAppClick(ZHIRWaitCustomerFollowUserListEntity zhirWaitCustomerFollowUserListEntity) {
                openWhatsApp(zhirWaitCustomerFollowUserListEntity);
            }

            @Override
            public void OnItemCopyAppLinkClick(ZHIRWaitCustomerFollowUserListEntity zhirWaitCustomerFollowUserListEntity) {
                copyAppLink(zhirWaitCustomerFollowUserListEntity);
            }
        });
    }

    private void initData() {
        if (mPresenter != null) {
            mPresenter.getLatestWaitCustomerFollowUserListV2(getActivity(), true);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData();
        });
        return emptyView;
    }

    @SuppressLint("InflateParams")
    private void showSendSmsView(ZHIRWaitCustomerFollowUserListEntity zhirWaitCustomerFollowUserListEntity) {
        try {
            if (mZHISubmitRecRecallView == null) {
                mZHISubmitRecRecallView = new ZHISubmitRecRecallView(getActivity());
            }
            View parentView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_recall, null);
            mZHISubmitRecRecallView.setName(zhirWaitCustomerFollowUserListEntity.getProduct());
            mZHISubmitRecRecallView.setContact(zhirWaitCustomerFollowUserListEntity.getMobile());
            mZHISubmitRecRecallView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String copyAppLink(ZHIRWaitCustomerFollowUserListEntity zhirWaitCustomerFollowUserListEntity) {
        String line = zhirWaitCustomerFollowUserListEntity.getProduct() + " " + MyApplication.getAppContext().getString(R.string.download_link_flag);
        String line0 = zhirWaitCustomerFollowUserListEntity.getUrl();
        String content = line + "\n" + line0 + "\n";

        ZHISysShareHelper.copyToClipboard(getActivity(), content);

        return content;
    }

    private void openWhatsApp(ZHIRWaitCustomerFollowUserListEntity zhirWaitCustomerFollowUserListEntity) {
        String content = copyAppLink(zhirWaitCustomerFollowUserListEntity);
        ZHISysShareHelper.sendMsgWithWhatsApp(getActivity(), ZHIProjectConfigs.PRE_PHONE + zhirWaitCustomerFollowUserListEntity.getMobile(), content);
    }
}